//
//  CS_Page_TextSelectView.m
//  CSUIKit
//
//  Created by SoulFireWang on 2017/5/22.
//  Copyright © 2017年 SoulFireWang. All rights reserved.
//

#import "CS_Page_TextSelectView.h"
#import "CS_Pop_Progress.h"
#import "CSSelectViewController.h"

@interface CS_Page_TextSelectView()

@property (nonatomic, strong) CS_Page_TextFieldView *pageTextFieldView;

@property (nonatomic, strong) UIButton *button;

@property (nonatomic, strong) CS_Pop_SingleSelectView *popSingleSelectView;

@property (nonatomic, strong) CS_Pop_MultitpleSelectView *popMultitpleSelectView;

@property (nonatomic, strong) CSSelectViewController *viewController;

@property (nonatomic, strong) CSPageEngine *pageEngine;

@property (nonatomic, assign) CSTextFieldType textFieldType;

@property (nonatomic,strong) CS_Pop_Progress *popToastView;

@end

@implementation CS_Page_TextSelectView

-(NSInteger)viewType{
    return CSViewType_PageTextSelectView;
}

+(instancetype)pageTextSelectView:(NSString *)placeHolder selectItems:(NSArray *)selectItems{
    CS_Page_TextSelectView *pageTextSelectView = [CS_Page_TextSelectView new];
    pageTextSelectView.selectItems = selectItems;
    pageTextSelectView.placeHolder = placeHolder;
    return pageTextSelectView;
}

+(instancetype)pageTextSelectView:(NSString *)placeHolder selectItems:(NSArray *)selectItems textFieldType:(NSInteger)textFieldType{
    
    CS_Page_TextSelectView *pageTextSelectView = [CS_Page_TextSelectView new];
    pageTextSelectView.selectItems = selectItems;
    pageTextSelectView.placeHolder = placeHolder;
    pageTextSelectView.textFieldType = textFieldType;
    
    return pageTextSelectView;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    CGFloat localY = 0;
    
    localY = [self.pageTextFieldView layoutView:self primaryY:localY];
    self.frame = CGRectMake(0, primaryY, kWindowW, localY);
    self.button.frame = CGRectMake(0, 0, kWindowW, localY);
    [self addSubview:self.button];
    
    [superView addSubview:self];
    
    return primaryY + localY;
}

-(void)setContentValue:(NSString *)contentValue{
    self.pageTextFieldView.value = contentValue;
    _contentValue = contentValue;
}

-(void)reset{
    
    self.value = @"";
    self.contentValue = @"";
    self.pageTextFieldView.value = @"";
    [self.pageTextFieldView reset];
    
}

-(void)selectClick{
    
    if (self.selectItems == nil || self.selectItems.count <= 0) {
        
        [self.pageTextFieldView showError];
        if (self.selectEmptyErrorHandler) {
            self.selectEmptyErrorHandler(self.superViewController);
        }
        
    }else if (self.isMutiltpleSelect) {
        
        if(self.superViewController){
            
            for (int i = 0; i < self.selectItems.count; i++) {
                
                CSSelectableModel *model = [self.selectItems objectAtIndex:i];
                model.isSelected = NO;
                
                for (int j = 0; j < model.subItems.count; j++) {
                    CSSelectableModel *subModel =  [model.subItems objectAtIndex:j];
                    subModel.isSelected = NO;
                }
                
            }
            
            [self.superViewController.navigationController pushViewController:self.viewController animated:YES];
        }
    } else{
        [self.popSingleSelectView bottomShow];
    }
    
}

/**
 * 选中选项
 */
-(void)setSelectItems:(NSArray *)selectItems{
    
    _selectItems = selectItems;
    
    WS(weakSelf);
    
    //如果是多选
    if (_isMutiltpleSelect) {
        
        _viewController = [CSSelectViewController selectViewController:self.placeHolder selectItems:self.selectItems savedHandler:^(NSString *selectedNamesString, NSString *selectedCodesString) {
            
            weakSelf.pageTextFieldView.value = selectedNamesString;
            weakSelf.value = selectedCodesString;
            
        }];
        
    }
    else{//单选
        
        if (self.selectItems == nil
            || self.selectItems.count <= 0) {
            _popSingleSelectView = [CS_Pop_SingleSelectView pop_SingleSelectViewForModel:self.placeHolder selectItems:self.selectItems selectHandler:^(NSInteger section, NSInteger index, CSSelectableModel *model) {
                
                weakSelf.pageTextFieldView.value = model.name == nil ? model.content : model.name;
                weakSelf.value = [NSString stringWithFormat:@"%ld", index];
                
                if (weakSelf.selectHandler2) {
                    weakSelf.selectHandler2(section, index, model);
                }
            }];
        }else{
            
            id selectItems = [self.selectItems objectAtIndex:0];
            if ([selectItems isKindOfClass:[CSSelectableModel class]]) {
                _popSingleSelectView = [CS_Pop_SingleSelectView pop_SingleSelectViewForModel:self.placeHolder selectItems:self.selectItems selectHandler:^(NSInteger section, NSInteger index, CSSelectableModel *model) {
                    weakSelf.pageTextFieldView.value = model.name == nil ? model.content : model.name;
                    weakSelf.value = model.code;
                    if (weakSelf.selectHandler2) {
                        weakSelf.selectHandler2(section, index, model);
                    }
                    
                }];
            }else{
                _popSingleSelectView = [CS_Pop_SingleSelectView pop_SingleSelectView:self.placeHolder selectItems:self.selectItems selectHandler:^(NSInteger section, NSInteger index, NSString *selectString) {
                    weakSelf.pageTextFieldView.value = selectString;
                    weakSelf.value = [NSString stringWithFormat:@"%ld", index];
                    if (weakSelf.selectHandler) {
                        weakSelf.selectHandler(section, index, selectString);
                    }
                    
                }];
            }
            
        }
        
    }
    
    
}

/**
 * 选项分级显示
 */
-(BOOL)isLeveledSelectItems{
    
    for (int i = 0; i < self.selectItems.count; i++) {
        
        id selectItem = [self.selectItems objectAtIndex:i];
        if ([selectItem isKindOfClass:[CSSelectableModel class]]) {
            
            CSSelectableModel *selectableModel = selectItem;
            
            if (selectableModel.subItems && selectableModel.subItems.count > 0) {
                return YES;
            }
        }
    }
    
    return NO;
}

-(void)setSeperateKeyString:(NSString *)seperateKeyString{
    
    super.seperateKeyString = seperateKeyString;
    _pageTextFieldView.seperateKeyString = seperateKeyString;
    
}

-(CS_Page_TextFieldView *)pageTextFieldView{
    if (_pageTextFieldView == nil) {
        _pageTextFieldView = [CS_Page_TextFieldView textFiledWithPlaceHolder:self.placeHolder keyboardType:UIKeyboardTypeDefault];
        _pageTextFieldView.isDownArrowRequired = YES;
        _pageTextFieldView.textFieldType = self.textFieldType;
        _pageTextFieldView.seperateKeyString = self.seperateKeyString;
    }
    return _pageTextFieldView;
}

-(UIButton *)button{
    if (_button == nil) {
        _button = [[UIButton alloc]init];
        [_button addTarget:self action:@selector(selectClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

-(CSSelectViewController *)viewController{
    
    if (_viewController == nil) {
        WS(weakSelf);
        _viewController = [CSSelectViewController selectViewController:self.placeHolder selectItems:self.selectItems savedHandler:^(NSString *selectedNamesString, NSString *selectedCodesString) {
            
            weakSelf.pageTextFieldView.value = selectedNamesString;
            weakSelf.value = selectedCodesString;
            weakSelf.contentValue = selectedNamesString;
            
        }];
    }
    
    return _viewController;
    
}

-(CS_Pop_SingleSelectView *)popSingleSelectView{
    
    if (_popSingleSelectView == nil) {
        WS(weakSelf);
        
        if (self.selectItems == nil
            || self.selectItems.count <= 0) {
            _popSingleSelectView = [CS_Pop_SingleSelectView pop_SingleSelectViewForModel:self.placeHolder selectItems:self.selectItems selectHandler:^(NSInteger section, NSInteger index, CSSelectableModel *model) {
                
                weakSelf.pageTextFieldView.value = model.name == nil ? model.content : model.name;
                weakSelf.value = [NSString stringWithFormat:@"%ld", index];
                
                if (weakSelf.selectHandler2) {
                    weakSelf.selectHandler2(section, index, model);
                }
            }];
        }else{
            
            id selectItems = [self.selectItems objectAtIndex:0];
            if ([selectItems isKindOfClass:[CSSelectableModel class]]) {
                _popSingleSelectView = [CS_Pop_SingleSelectView pop_SingleSelectViewForModel:self.placeHolder selectItems:self.selectItems selectHandler:^(NSInteger section, NSInteger index, CSSelectableModel *model) {
                    weakSelf.pageTextFieldView.value = model.name == nil ? model.content : model.name;
                    weakSelf.value = model.code;
                    weakSelf.contentValue = model.name == nil ? model.content : model.name;
                    if (weakSelf.selectHandler2) {
                        weakSelf.selectHandler2(section, index, model);
                    }
                    
                }];
            }else{
                _popSingleSelectView = [CS_Pop_SingleSelectView pop_SingleSelectView:self.placeHolder selectItems:self.selectItems selectHandler:^(NSInteger section, NSInteger index, NSString *selectString) {
                    weakSelf.pageTextFieldView.value = selectString;
                    weakSelf.value = [NSString stringWithFormat:@"%ld", index];
                    weakSelf.contentValue = selectString;
                    if (weakSelf.selectHandler) {
                        weakSelf.selectHandler(section, index, selectString);
                    }
                    
                }];
            }
            
        }
        
    }
    return _popSingleSelectView;
}

-(CS_Pop_MultitpleSelectView *)popMultitpleSelectView{
    
    if (_popMultitpleSelectView == nil) {
        _popMultitpleSelectView = [CS_Pop_MultitpleSelectView pop_multitpleSelectView:self.selectItems titleName:self.placeHolder selectCompletedHandler:^(NSString *resultNames, NSString *resultCodes) {
            
        }];
    }
    return _popMultitpleSelectView;
    
}

@end
